<?php

namespace app\admin\controller\caiwu;

use app\common\controller\Backend;

/**
 * 账单类别
 *
 * @icon  fa fa-copy
 */
class Billcate extends Backend
{
    /**
     * View
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,null);
            $type = input('type',1);
            $list = db('bill_cate')
                ->where('type',$type)
                ->where('is_delete',-1)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $data = $list->items();
            foreach ($data as &$item){
                $item['update_time2'] = mb_substr($item['update_time'],0,-3);
            }
            $result = array("total" => $list->total(), "rows" => $data,"extend"=>[]);

            return json($result);
        }

        return $this->view->fetch();

    }

    /**
     * Add
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if(empty($params['bianhao'])){
                $this->error(__("Code Can't be empty"));
            }
            $params['bianhao'] = trim($params['bianhao']);
            $res= db('bill_cate')->where('bianhao',$params['bianhao'])->find();
            if($res){
                $this->error(__('This code already exists'));
            }
            if(empty($params['cate_name'])){
                $this->error(__("Category Can't be empty"));
            }

            $params['create_time'] = $params['update_time'] = date("Y-m-d H:i:s");
            unset($params['id']);
            db('bill_cate')->insertGetId($params);
            $this->success(__('Add Success'));
        }
        $this->view->assign('id',0);
        $this->view->assign('data',[]);
        $this->view->assign('type',1);
        return $this->view->fetch('addcate');

    }

    /**
     * Edit
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            /*if(empty($params['bianhao'])){
                $this->error(__("Code Can't be empty"));
            }*/
            /*$params['bianhao'] = trim($params['bianhao']);
            $res= db('bill_cate')->where('bianhao',$params['bianhao'])->find();
            if($res && $res['id'] != $params['id']){
                $this->error(__('This code already exists'));
            }*/
            if(empty($params['cate_name'])){
                $this->error(__("Category Can't be empty"));
            }


            $params['update_time'] = date("Y-m-d H:i:s");
            db('bill_cate')->update($params);
            $this->success(__('Operation Success'));
        }

        $data = db('bill_cate')->find($ids);
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        $this->view->assign('type',$data['type']);
        return $this->view->fetch('addcate');
    }
    /**
     * del
     */
    public function del($ids = null)
    {
        if(empty($ids)){
            $this->error("参数有误");
        }
        $idArr = explode(',',$ids);
        foreach ($idArr as $id){
            //是否记录过账单
            $cateData = db('bill_cate')->find($id);
            $hasOne = db('bill_record')->where('cate_bianhao',$cateData['bianhao'])->find();
            if($hasOne){
                db('bill_cate')->where('id',$id)->update(['is_delete'=>1]);
            }else{
                db('bill_cate')->where('id',$id)->delete();
            }
        }
        $this->success("删除成功");
    }
}
